<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;
class UserController extends BaseController{
    public function add(){
       $this->assign('title','管理员添加');
       $this->assign('url',U('User/insert'));
       $this->assign('grouplist',D('Auth_group')->select());
       $this->assign('info','添加');
       $this->display('User_info');
    }
    public function insert(){
        $where['name']=I('post.name');
        $nameinfo=D('User')->where($where)->find();
        if ($nameinfo>0){
            $this->error('系统用户名已存在添加失败！');
        }else {
            if (D('User')->getinfo()){
                $this->success('添加成功！',U('User/View'));
            }else {
                $this->error('添加失败！');
            }
        }
        
    }
    public function edit(){
        $data['uid']=I("get.id");
        $groupId=D('Auth_group_access')->where($data)->find();       
        $this->assign('groupId',$groupId);
        $this->assign('title','管理员更新');
        $this->assign('url',U('User/update'));
        $this->assign('editinfo',D('User')->editInfo());
        $this->assign('grouplist',D('Auth_group')->select());
        $this->assign('info','更新');
        $this->display('User_info');
    }
    public function update(){  
        $data['group_id']=$_POST['group_id'];
        $map['uid']=I("post.id");      
        $result=D('Auth_group_access')->where($map)->save($data);
        if(false !== $result || 0 !== $result){ 
            $res=D('User')->updateInfo();
            if (false !==   $res || 0 !==   $res){
                $this->success('更新成功！',U('User/View'));
            }          
        }else {
            $this->error('更新失败！');
        }
        
       
    }
    
    public function delete(){     
        $where['uid']=I("get.id");
        $result=D('Auth_group_access')->where($where)->delete();
        if($result){
            if(D('User')->delInfo()){
                $data['state']=1;
                $data['msg']='删除成功';
                $this->ajaxReturn($data);
            }else {
                $data['state']=0;
                $data['msg']='删除失败';
                $this->ajaxReturn($data);
            }
        }else {
            $data['state']=0;
            $data['msg']='删除失败';
            $this->ajaxReturn($data);
        }
       
    }
      
    public function view(){
        $this->assign('title','管理员列表');      
        $this->assign('info','添加');
        $this->assign('userlist',D('User')->select());
        $this->display('User_list');
    }
    
}